namespace BM.Tools.WebControls.MForm.Controls.Helper
{
    using System;    

    using BM.Tools.WebControls.MForm.Generator;
    using BM.Tools.WebControls.MForm.Resources;

    /// <summary>
    /// A button that can be used to add another item to the given offspring
    /// </summary>
    public class AddButton : Button
    {
        private const string AddSuffix = "Add";        

        /// <summary>
        /// Initializes a new instance of the <see cref="AddButton"/> class. 
        /// </summary>
        /// <param name="owner">
        /// The owner.
        /// </param>
        public AddButton(Offspring owner)
        {
            Owner = owner;
            AppendDefaultCssClass();
        }

        /// <summary>
        /// Gets the Owner of the button.
        /// </summary>
        /// <value>
        /// The owner.
        /// </value>
        public Offspring Owner
        {
            get; 
            private set;
        }

        /// <summary>
        /// Gets 'add' or 'set' translated text 
        /// </summary>
        public string ActionText
        {
            get
            {
                return LanguageResource.ResourceManager.GetString(ActionType.ToString());                
            }
        }

        /// <summary>
        /// Gets or sets ActionType.
        /// </summary>
        /// <value>
        /// The action type.
        /// </value>
        public ButtonActionType ActionType
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets ClientID.
        /// </summary>
        /// <value>
        /// The client id.
        /// </value>
        public override string ClientID
        {
            get
            {
                return String.Concat(Owner.CreateClientID(1), "__", AddSuffix);
            }
        }

        /// <summary>
        /// Gets or sets OnClientClick.
        /// </summary>
        /// <value>
        /// The on client click.
        /// </value>
        public override string OnClientClick
        {
            get
            {
                if (base.OnClientClick == null)
                {
                    return "MBasis.add(this.parentNode.getAttribute('name')); return false;";
                }

                return base.OnClientClick;
            }

            set
            {
                base.OnClientClick = value;
            }
        }

        /// <summary>
        /// Gets or sets Text.
        /// </summary>
        /// <value>
        /// The text that is displayed within the button.
        /// </value>
        public override string Text
        {
            get
            {
                if (base.Text == null)
                {
                    return String.Concat(ActionText, " ", Owner.DisplayName);
                }

                return base.Text;
            }

            set
            {
                base.Text = value;
            }
        }

        /// <summary>
        /// Gets or sets Visible.
        /// </summary>
        /// <value>
        /// The visible.
        /// </value>
        public override bool Visible
        {
            get
            {
                if (Owner.Mode == FormBuilderMode.View)
                {
                    return false;
                }

                if (!Owner.HasVariableOccurs)
                {
                    return false;
                }

                if (!Owner.Visible)
                {
                    return false;
                }

                return base.Visible;
            }

            set
            {
                base.Visible = value;
            }
        }

        /// <summary>
        /// Adds id and name attribute
        /// </summary>
        /// <param name="writer">
        /// The writer.
        /// </param>         
        protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
        {
            base.AddAttributesToRender(writer);
            writer.AddAttribute("name", Owner.ParentItem.ClientID + "/" + Owner.Name);
        }

        private void AppendDefaultCssClass()
        {
            CssClass = AddSuffix;
        } 
    }  
}